package com.dudu.access;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

import com.dudu.bean.ResponseSendVoiceBean;
import com.dudu.service.ILogService;
import com.dudu.service.impl.LogService;
import com.dudu.utils.DateUtils;
/**
 * 文件上传
 * @author gaoxs
 *
 */
public class ImsUploadFile {

    private static ILogService logService = new LogService();

    // 上传语音文件
    @SuppressWarnings("rawtypes")
    public static ResponseSendVoiceBean post(String url, Object... objs) throws IOException {
	ResponseSendVoiceBean responseSendVoice = new ResponseSendVoiceBean();
	// 开始时间
	long begin = 0;
	// 结束时间
	long end = 0;
	String currentTime = DateUtils.getCurrentTime();

	// 记录发起的开始时间
	begin = Calendar.getInstance().getTimeInMillis();
	Map paramMap = (Map) objs[0];
	PostMethod postMethod = new PostMethod(url);
	Part[] part = null;
	if (objs.length > 1) {
	    part = new Part[paramMap.size() + 1];
	    FilePart fp = new FilePart("Myfile", new File(objs[1].toString()));
	    part[part.length - 1] = fp;
	} else {
	    part = new Part[paramMap.size()];
	}
	Iterator iterator = paramMap.keySet().iterator();
	for (int i = 0; iterator.hasNext(); i++) {
	    Object key = iterator.next();
	    StringPart sp = new StringPart(key.toString(), paramMap.get(key).toString());
	    part[i] = sp;
	}
	MultipartRequestEntity mrp = new MultipartRequestEntity(part, postMethod.getParams());

	postMethod.setRequestEntity(mrp);
	HttpClient httpClient = new HttpClient();
	try {
	    int result = httpClient.executeMethod(postMethod);
	    if (200 == result) {
		String responseStr = postMethod.getResponseBodyAsString();
		// 记录结束时间
		if (responseStr.startsWith("http://110.84.128.78:8088/notice")) {
		    responseSendVoice.setResult("0");
		    responseSendVoice.setFileName(responseStr);
		} else {
		    responseSendVoice.setResult("-1");
		    responseSendVoice.setFileName(responseStr);
		}

	    } else {
		responseSendVoice.setResult("-1");
		responseSendVoice.setFileName("");
	    }

	} catch (HttpException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}
	end = Calendar.getInstance().getTimeInMillis();
	Double during = Double.valueOf((end - begin)) / 1000;
	logService.logIms(during, "UploadFile", "上传文件-ims", responseSendVoice.getFileName(), responseSendVoice.getResult(), currentTime, "IMS", "");
	return responseSendVoice;
    }

    public static void main(String[] argn) {/*
					     * try { Map<String,String> paramMap
					     * =new HashMap<String,String>();
					     * paramMap
					     * .put("Name","111145623545581");
					     * paramMap.put("Spid",Config.SPID);
					     * paramMap
					     * .put("Appid",Config.APPID); try {
					     * paramMap
					     * .put("Passwd",ServiceBase.
					     * sha1Encrypt(Config.PWD)); } catch
					     * (NoSuchAlgorithmException e) { //
					     * TODO Auto-generated catch block
					     * e.printStackTrace(); } String
					     * result= UploadFile.post(Config.
					     * SERVICE_UPLOADVOICE
					     * ,paramMap,"F://20135.mp3"); }
					     * catch (IOException e) { // TODO
					     * Auto-generated catch block
					     * e.printStackTrace(); }
					     */
    }

}
